﻿using EquipmentManager.Entities;
using EquipmentManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EquipmentManager.BOLogic
{
    public class RegionBO : BOBase
    {
        public List<Region> GetRegions(long DirectorateId)
        {
            try
            {
                var regions = db.Set<Region>().Where(r => r.Directorate.Id == DirectorateId && 
                    r.Status == EntityStatus.Active).OrderBy(r => r.Name).ToList();
                return regions;
            }
            catch (Exception ex)
            {
                return new List<Region>();
            }
        }

        public Region GetOperRegion()
        {
            try
            {
                var region = db.Set<Region>().Single(r => r.Id == User.Region.Id && r.Status == EntityStatus.Active);
                return region;
            }
            catch (Exception ex)
            {
                return new Region();
            }
        }

        public Region GetRegion(long RegionId)
        {
            try
            {
                var region = db.Set<Region>().Single(r => r.Id == RegionId && r.Status == EntityStatus.Active);
                return region;
            }
            catch (Exception ex)
            {
                return new Region();
            }
        }

        public bool CreateNewRegion(CreateModel<Region> model)
        {
            try
            {
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (admin.Role == UserRole.Admin)
                {
                    model.Instance.Status = EntityStatus.Active;
                    model.Instance.Directorate = db.Set<Directorate>().Single(d => d.Id == model.ParentId);

                    db.Set<Region>().Add(model.Instance);
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool EditRegion(CreateModel<Region> model)
        {
            try
            {
                var region = db.Set<Region>().Single(c => c.Id == model.Instance.Id);
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (admin.Role == UserRole.Admin)
                {
                    // можно создавать новую измененную, а эту делать неактивной если что;)
                    region.Name = model.Instance.Name;
                    region.Description = model.Instance.Description;
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeleteRegion(long RegionId)
        {
            try
            {
                var region = db.Set<Region>().Single(c => c.Id == RegionId);
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (admin.Role == UserRole.Admin)
                {
                    region.Status = EntityStatus.Deleted;
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}